﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using serviceMaker.RssService;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;

namespace ViewModel.Rss
{
    public class DataRssSelected : DataRss
    {
        public DataRssSelected()
        {
            Selected = false;
        }

        public DataRssSelected(DataRss parent)
        {
            this.authors = parent.authors;
            this.description = parent.description;
            this.guid = parent.guid;
            this.id = parent.id;
            this.image = parent.image;
            this.last_importation = parent.last_importation;
            this.link = parent.link;
            this.name = parent.name;
            this.url = parent.url;
            Selected = false;
        }

       public bool Selected
       {get; set;}
    }



    public class AddRssViewModel : RssViewModel
    {

        public AddRssViewModel()
        {
            askForRssAvalaible();
            SelectionChange = new RelayCommand<SelectionChangedEventArgs>(ExecuteSelectionChange, CanExecuteSelectionChange);
            Save = new RelayCommand(ExecuteSave, CanExecuteSave);
        }



        #region Property
        private string urlFeed;
        public string UrlFeed
        {
            get {return urlFeed;}
            set
            {
                if (value != urlFeed)
                {
                    urlFeed = value;
                    RaisePropertyChanged("UrlFeed");
                }
            }
        }

        private ObservableCollection<DataRssSelected> availableFeeds ;
        public ObservableCollection<DataRssSelected> AvailableFeeds
        {
            get 
            { 
                return availableFeeds;
            }
            set
            {
                if (value != availableFeeds)
                {
                    availableFeeds = value;
                    RaisePropertyChanged("AvailableFeeds");
                }
            }
        }


        private ObservableCollection<object> selectedFeeds;
        public ObservableCollection<object> SelectedFeeds
        {
            get
            {
                return selectedFeeds;
            }
            set
            {
                if (value != selectedFeeds)
                {
                    selectedFeeds = value;
                    RaisePropertyChanged("SelectedFeeds");
                }
            }
        }


       private void askForRssAvalaible()
       {
           RssService.CookieContainer = clientInfos.cc;
           RssService.getAllRssCompleted += (obj, arg) => {
               if (arg != null && arg.Error == null && arg.Result != null)
               {
                   ObservableCollection<DataRssSelected> result = new ObservableCollection<DataRssSelected>();
                   List<DataRss> listRep = arg.Result.getAllRssResult;
                   foreach (DataRss tmp in listRep)
                       result.Add(new DataRssSelected(tmp));
                   AvailableFeeds = result;
               }
           };
           try
           {
               RssService.getAllRssAsync(new getAllRssRequest());
           }
           catch (Exception e)
           {
               Console.WriteLine("Unable to call async GetAllRss " + e.Message);
           }
       }

       //private ObservableCollection<System.Guid> listId;
       //public ObservableCollection<System.Guid> ListId
       //{
       //    get
       //    {
       //        return listId;
       //    }
       //    set
       //    {
       //        if (value != listId)
       //        {
       //            listId = value;
       //            RaisePropertyChanged("ListId");
       //        }
       //    }
       //}
        #endregion


        #region Command

       public ICommand SelectionChange
       { get; set; }

       public void ExecuteSelectionChange(SelectionChangedEventArgs arg)
       {
           //if (arg != null && arg.AddedItems != null && arg.AddedItems.Count > 0)
           //{
           //    ListId = new ObservableCollection<Guid>();
           //    foreach (DataRssSelected dataRss in arg.AddedItems)
           //        ListId.Add(dataRss.id);
           //}
       }

       public bool CanExecuteSelectionChange(SelectionChangedEventArgs arg)
       {
           return true;
       }

       public ICommand Save
       { get; set;}

       public void ExecuteSave()
       { 
           List<string>   listUrl = new List<string>();
           List<Guid> listTmp = new List<Guid>();

           if (UrlFeed != null && UrlFeed != "")
               listUrl.Add(UrlFeed);
           RssService.CookieContainer = clientInfos.cc;
           if (SelectedFeeds != null)
           {
               foreach (object obj in SelectedFeeds)
                   if (obj is DataRssSelected)
                       listTmp.Add(((DataRssSelected)obj).id);
           }
           RssService.addListRssToDirectoryCompleted += (obj, arg) =>
           {
               if (arg != null && arg.Error == null && arg.Result != null)
               {
                   Messenger.Default.Send<string>("FluxRss", "reload");
               }


           };
           if (clientInfos.userRootDirectory != null)
           RssService.addListRssToDirectoryAsync(new addListRssToDirectoryRequest(listTmp, listUrl, clientInfos.userRootDirectory));
           Messenger.Default.Send<string>("AnswerSaveRss", "AnswerSaveRss");
           

       }

       public bool CanExecuteSave()
       { return true; }
        #endregion


    }
}
